// Widgets/Personals/Matings/Matings.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetMatingInfo , queryPetInfo } from '../../../api/pet';
import { queryAppInforMation } from '../../../api/brand';
import { throttle } from '../../../utils/util';
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    screenHeight: wx.getSystemInfoSync().screenHeight,
    CustomBar: app.globalData.CustomBar,
    tabbarHeight: wx.getSystemInfoSync().screenHeight - wx.getSystemInfoSync().safeArea.bottom + 60,
    page: 0,
    dataList: [],
    scrollHeight: 0,
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
    refresher: false,
    petId: '',
    petName: '全部宠物',
    matingStatus: '', // 配种状态
    matingStatusDesc: '配种状态', // 
    show: false,
    petList: [],
    statusList: [
      { id: '0', name: '进行中'},{ id: '1', name: '配种成功'},{ id: '0', name: '配种失败'},
    ],
    popActive: '',
    radio: 0,
  },
  lifetimes: {
    attached: function() {
      let _this = this;
      // 在组件实例进入页面节点树时执行
      console.log('动态模块')
      _this._queryPetMatingInfo();
      _this.getUserPet();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 加载更多数据
    onLoadMore: throttle(function(){
      console.log('上拉加载')
      if(this.data.isReachBottom){
        this._queryPetMatingInfo();
      }
    },500),
    onPullDownRefresh: function(){
      let _this = this;
      _this.setData({
        page: 0,
        isReachBottom: false,
        noMore: false,
        loading: false,
        loadingFailed: false,
      })
      this._queryPetMatingInfo();
      setTimeout(()=>{
        wx.stopPullDownRefresh({
          success: (res) => {
            console.log(res,'刷新成功')
            _this.setData({
              refresher: false
            })
            wx.showToast({
              title: '刷新成功',
              icon: 'none',
            })
          },
        })
      },1500)
    },
    _queryPetMatingInfo(){
      let _this = this;
      queryPetMatingInfo({petId: _this.data.petId,matingStatus: _this.data.matingStatus,page: _this.data.page})
      .then(res => {
        if(res.success){
          var list = _this.data.dataList;
          var arr = res.data.content;
          if(_this.data.page == 0){
            list = arr;
          }else{
            list = list.concat(arr);
          }
          _this.setData({
            dataList: list,
            total: res.data.totalElements,
            page: _this.data.page+1,
          })
          if(_this.data.page * 10 >= res.data.totalElements){
            _this.setData({
              isReachBottom: false,
              noMore: true,
              loading: false,
              loadingFailed: false,
            })
          }else{
            _this.setData({
              isReachBottom: true,
              noMore: false,
              loading: false,
              loadingFailed: false,
            })
          }
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
    getUserPet(){
      let _this = this;
      queryPetInfo({userId: wx.getStorageSync('userId'),})
      .then(res => {
        if(res.success){
          _this.setData({
            petList: res.data.content,
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
    // 页面跳转
    goNext(e){
      let _this = this;
      let type = e.target.dataset.type||e.currentTarget.dataset.type;
      let url = e.target.dataset.name||e.currentTarget.dataset.name;
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      console.log(url)
      // return
      if(item){
        if(type == 1){
          wx.switchTab({
            url: `${url}`,
          })
        }else{
          wx.navigateTo({
            url: `${url}?item=${JSON.stringify(item)}`,
          })
        }
      }else{
        if(type == 1){
          wx.switchTab({
            url: `${url}`,
          })
        }else{
          wx.navigateTo({
            url: `${url}`,
            events: {
              'updateMatings': function(data){
                _this.setData({
                  page: 0,
                  isReachBottom: false,
                  noMore: false,
                  loading: false,
                  loadingFailed: false,
                })
                _this._queryPetMatingInfo();
              }
            }
          })
        }
      }
    },
    getApplication(e){
      let _this = this;
      let item = e.target.dataset.type||e.currentTarget.dataset.type;
      let title = e.target.dataset.name||e.currentTarget.dataset.name;
      wx.navigateTo({
        url: `/pages/System/CommonProblem/CommonProblem?item=${item}&title=${title}`,
      })
      // queryAppInforMation({infoType: '2',title: item,page: 0})
      // .then(res => {
      //   if(res.success){
      //     if(res.data.content.length > 0){
      //       var itm = res.data.content[0];
      //       if(res.data.content[0].imgUrl){
      //         wx.navigateTo({
      //           url: `/pages/Link/LinkImg/LinkImg?url=${encodeURIComponent(itm.imgUrl)}&title=${JSON.stringify(item)}`,
      //         })
      //       }else if(itm.jumpUrl && itm.jumpUrl.length > 4 && itm.jumpUrl.substring(0,4) == 'http'){
      //         wx.navigateTo({
      //           url: `/pages/Link/Link/Link?url=${encodeURIComponent(itm.jumpUrl)}&title=${JSON.stringify(item)}`,
      //         })
      //       }else if(res.data.content[0].content){
      //         wx.navigateTo({
      //           url: `/pages/Link/LinkText/LinkText?content=${JSON.stringify(itm.content)}&title=${JSON.stringify(item)}`,
      //         })
      //       }
      //     }else{
      //       wx.showToast({
      //         title: '敬请期待',
      //         icon: 'none',
      //       })
      //     }
      //   }else{
      //     wx.showToast({
      //       title: res.errMessage,
      //       icon: 'none',
      //     })
      //   }
      // })
    },
    selPet(){
      this.setData({show: true,popActive: 1});
    },
    selStatus(){
      this.setData({show: true,popActive: 2});
    },
    onClose(){
      this.setData({show: false})
    },
    onRadioChange(event){
      console.log(event,'event')
      this.setData({
        radio: event.detail,
      });
    },
    onRadioClick(event) {
      const { name } = event.currentTarget.dataset;
      this.setData({
        radio: name,
      });
    },
    reset(){
      let _this = this;
      _this.setData({
        petId: _this.data.popActive == 1 ? '' : _this.data.petId,
        matingStatus: _this.data.popActive == 2 ? '' : _this.data.matingStatus,
        page: 0,
        isReachBottom: false,
        noMore: false,
        loading: false,
        loadingFailed: false,
        show: false,
      })
      _this._queryPetMatingInfo();
    },
    submitParams(){
      let _this = this;
      _this.setData({
        petId: _this.data.popActive == 1 ? _this.data.petList[_this.data.radio].id : _this.data.petId,
        matingStatus: _this.data.popActive == 2 ? _this.data.statusList[_this.data.radio].id : _this.data.matingStatus,
        page: 0,
        isReachBottom: false,
        noMore: false,
        loading: false,
        loadingFailed: false,
        show: false,
      })
      _this._queryPetMatingInfo();
    },
  }
})